<?php
/**
 * @description:
 * @author     : Geomen
 * @time       : 2019/1/31 17:53
 */


namespace App\Presenters;


use App\Components\Helper;
use Illuminate\Http\Request;

class AppPresenter
{
    protected $request;
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 左侧一级菜单,如果请求地址在$sub里,则添加open,否则如果请求地址跟$url一样,则添加active
     *
     * @param  string $url 菜单地址
     * @param   array $sub
     *
     * @return string
     */
    public function setParentClass($url, $sub)
    {
        $path = Helper::formatUrl($this->request->path());
        if (in_array($path,$sub)){
            return 'open';
        }elseif ($path == $url){
            return 'active';
        }
        return '';
    }

    /**
     * 显示子菜单列表
     *
     * @param array $sub 子菜单数组
     *
     * @return string
     */
    public function showDropdownToggle($sub)
    {
        if (count($sub)){
            return "dropdown-toggle";
        }
        return '';
    }

    /**
     * 单个子菜单选中和显示
     *
     * @param string $url
     * @param int $is_hidden 是否隐藏
     *
     * @return string
     */
    public function showSubHiddenMenu($url,$is_hidden)
    {
        $path = Helper::formatUrl($this->request->path());
        if ($path == $url){
            return 'active';
        }elseif ($is_hidden){
            return 'hidden';
        }else{
            return '';
        }
    }
}